package top.superflow.flow.common

import top.superflow.exception.Exceptable
import top.superflow.flow.condition.ExpressionEngine
import top.superflow.flow.runtime.processprovider.OriginalProcessProxy
import top.superflow.utils.MetadataUtils

class StaticClassOriginalProcessProxy
    extends OriginalProcessProxy
    with OriginalProcessProxyMethodTools
    with Exceptable
{

  private def extractCondition : String = {
    val condition = MetadataUtils.get(this.getMetadata, MetadataUtils.META_DATA_NAME_CONDITION)
    if(condition == null){
      throwRequestUnsupportedException("Expecting condition item in the metadata, but there is null")
    }
    condition.toString
  }
  def execute(params: Map[String, Any]): Any = {
    val condition = extractCondition
    val extCondition = ExpressionEngine.getInstance.convert(condition)
    extCondition.evaluate(params)
  }


  protected def getObject: Any = {
    getProcessClass.newInstance()
  }

  protected def getProcessClass: Class[_] = {
    val clz = MetadataUtils.getClass(getMetadata)
    if(clz == null){
      throwRequestUnsupportedException("There is no corresponding class under class for {}", MetadataUtils.get(getMetadata, MetadataUtils.META_DATA_NAME_CLASS))
    }
    clz
  }
}
